//
//  MainHeadCell.m
//  ZITHER
//
//  Created by weixin on 2018/10/16.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MainHeadCell.h"
#import "MeSetUsetImg.h"
#import "NetworkEngine.h"
#import "NetworkEngine+imageCloudManager.h"
#import "Ugc_Receive_VC_WX.h"
#import "Ugc_MyLaud_VC.h"
#import "MeSetUsetImgVC.h"

@implementation MainHeadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
//        BaseView *view =[[BaseView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 160)];
        self.backgroundColor = RGB(51, 51, 51);
//        [self addSubview:view];
        //    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
        //        BaseView *view = [[BaseView ]initWithFrame:CGRectMake(0, 0, kScreenW,100)];
        //        view.backgroundColor = RGB(52,alloc 52, 52);
        //        [self.view addSubview:view];
        //    }
    }else{
        self.backgroundColor = HexColor(0xf5f5f5);

    }

    
    [self setUI];
    [self SetMassonry];

}


-(void)setUI{
    [self addSubview:self.picUser];
    [self addSubview:self.labUserName];
    [self addSubview:self.labUserPhone];
    [self addSubview:self.btnReceive];
    
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
        [self addSubview:self.tipeLab];
        [self addSubview:self.VipLab];
    }

    
}

-(void)SetMassonry{
    
    [self.picUser mas_makeConstraints:^(MASConstraintMaker *make) {
        if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
            make.top.equalTo(self).offset(20);
            make.left.equalTo(self).offset(15);
            make.height.width.mas_equalTo(50);
        }else{
            make.top.equalTo(self).offset(20);
            make.left.equalTo(self).offset(15);
            make.height.width.mas_equalTo(80);
        }
    }];
    
    [self.labUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
            make.top.equalTo(self.picUser.mas_top).offset(3);
            make.left.equalTo(self.picUser.mas_right).offset(15);
        }else{
            make.centerY.equalTo(self.picUser.mas_centerY).offset(-15);
            make.left.equalTo(self.picUser.mas_right).offset(15);
        }
    }];
    
    [self.labUserPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
            make.bottom.equalTo(self.picUser.mas_bottom).offset(-3);
            make.left.equalTo(self.picUser.mas_right).offset(15);
        }else{
            make.centerY.equalTo(self.picUser.mas_centerY).offset(15);
            make.left.equalTo(self.picUser.mas_right).offset(15);
        }
    }];

    [self.btnReceive mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX).offset(0);
        make.width.mas_equalTo(kScreenW-30);
        if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
            make.top.equalTo(self.labUserPhone.mas_bottom).offset(30);
            make.height.mas_equalTo(44);
            make.bottom.equalTo(self).offset(0);
            self.btnReceive.hidden = NO;
        }else{
            make.top.equalTo(self.labUserPhone.mas_bottom).offset(20);
            make.height.mas_equalTo(0);
            make.bottom.equalTo(self).offset(0);
            self.btnReceive.hidden = YES;
        }
    }];
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
        [self.tipeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.labUserName.mas_centerY).offset(0);
            make.left.equalTo(self.labUserName.mas_right).offset(15);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(15);
        
        }];
        [self.VipLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.labUserName.mas_centerY).offset(0);
            make.left.equalTo(self.tipeLab.mas_right).offset(5);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(15);
        }];
        
    }

    
    
}


-(UIImageView *)picUser{
    
    if (!_picUser) {
        
        _picUser = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW/2-kScreenW/100*33/2, kScreenH/100*12, kScreenW/100*33, kScreenW/100*33)];
        [_picUser setCornerRadiusWithRadius:kScreenW/100*33/2 isMasks:YES];
        _picUser.backgroundColor = [UIColor randomColor];
        _picUser.userInteractionEnabled = YES;
        _picUser.contentMode = 2 ;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        @weakify(self);
        if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
            [_picUser setCornerRadiusWithRadius:25 isMasks:YES];

        }else{
            [_picUser setCornerRadiusWithRadius:40 isMasks:YES];
        }
        [[tap rac_gestureSignal] subscribeNext:^(id x) {
            @strongify(self);
            MeSetUsetImgVC *vc = [[MeSetUsetImgVC alloc]init];
            [self.getNav pushViewController:vc animated:YES];            
        }];
        [_picUser addGestureRecognizer:tap];
    }
    return _picUser;
}

-(QMUILabel *)labUserName{
    if (!_labUserName) {
        _labUserName = [[QMUILabel alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.picUser.frame)+25, kScreenW, 25)];
        _labUserName.userInteractionEnabled = YES;
        _labUserName.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
        _labUserName.textColor = [UIColor colorWithHex:@"333333"];
        _labUserName.text = @"用户名称";
        _labUserName.textAlignment = 1;
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] init];
        [[tap2 rac_gestureSignal] subscribeNext:^(id x) {
            NSLog(@"tap2");
            MeSetUserName *vc = [[MeSetUserName alloc]init];
            [self.getNav pushViewController:vc animated:YES];
//            [self revampedUserName];
        }];
        [_labUserName addGestureRecognizer:tap2];
        if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
            _labUserName.textColor = [UIColor whiteColor];
        }
    }
    return _labUserName;
}

-(void)revampedUserName{

    QMUIDialogTextFieldViewController *dialogViewController = [[QMUIDialogTextFieldViewController alloc] init];
    dialogViewController.title = @"请输入昵称";
    
    [dialogViewController addTextFieldWithTitle:@"名称" configurationHandler:^(QMUILabel *titleLabel, QMUITextField *textField, CALayer *separatorLayer) {
        textField.placeholder = @"不超过10个字符";
        textField.maximumTextLength = 10;
    }];
    
    dialogViewController.enablesSubmitButtonAutomatically = NO;// 为了演示效果与第二个 cell 的区分开，这里手动置为 NO，平时的默认值为 YES
    
    
    [dialogViewController addCancelButtonWithText:@"取消" block:nil];
    [dialogViewController addSubmitButtonWithText:@"确定" block:^(QMUIDialogTextFieldViewController *aDialogViewController) {
        NSString *name = [aDialogViewController.textFields.firstObject.text removeSpaceAndNewline];
        
        if (name.length > 0&&name.length <11) {
            
            /* TODO: ================= 修改名称 =================*/
            
            NSString *name = aDialogViewController.textFields.firstObject.text;
            @weakify(self);
            
            [[AccountService loadsaveUserInfo:name]subscribeNext:^(ResponseData  *x) {
                @strongify(self);
                if (x.success) {
                    kUSER_INFO_MANAGER.nickName = name;
                    [kUSER_INFO_MANAGER MyList];
                    //                    self.UserName.text = aDialogViewController.textFields.firstObject.text;
                    [Tools showTips:@"修改成功"];
                    
                }else{
                    [QMUITips showError:x.errorInfo];
                }
                
            }];
            
        } else {
            [Tools showTips:@"不能为空或者超过8个长度"];
        }
        [aDialogViewController hide];
    }];
    
    [dialogViewController show];
    self.currentTextFieldDialogViewController = dialogViewController;
}

-(QMUILabel *)labUserPhone{
    if (!_labUserPhone) {
        _labUserPhone = [[QMUILabel alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.labUserName.frame)+16, kScreenW, 18)];
        _labUserPhone.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13];
//        _labUserPhone.font = [UIFont fontWithName:@"Helvetica-Bold" size:22];
        _labUserPhone.text = @"13713855641";
        _labUserPhone.textColor = [UIColor colorWithHex:@"333333"];
        _labUserPhone.textAlignment = 1;
        if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
            _labUserPhone.textColor = [UIColor whiteColor];
        }
    }
    return _labUserPhone;
}

-(QMUIButton *)btnReceive{
    if (!_btnReceive) {
        _btnReceive = [[QMUIButton alloc]init];
        _btnReceive.frame = CGRectMake(14,CGRectGetMaxY(self.labUserPhone.frame)+kScreenH/100*12, kScreenW-28, 44);
        _btnReceive.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:17];
        [_btnReceive setCornerRadiusWithRadius:8 isMasks:YES];
        [_btnReceive setBorderWithColor:[UIColor colorWithHex:@"333333"] width:1];
        [_btnReceive setTitle:@"我的消息" forState:UIControlStateNormal];
        [_btnReceive setTitle:@"3条新消息" forState:UIControlStateSelected];
        [_btnReceive setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_btnReceive setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _btnReceive.backgroundColor = [UIColor whiteColor];
        if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
//            _btnReceive.hidden = YES;
        }
        @weakify(self);
        [[_btnReceive rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            
            Ugc_Receive_VC_WX *vc = [[Ugc_Receive_VC_WX alloc]init];
            [self.getNav pushViewController:vc animated:YES];
            
            if (!self.btnReceive.selected) {
                self.btnReceive.selected = !self.btnReceive.selected;
            }else{
                
            }
            
        }];
    }
    return _btnReceive;
}

-(BaseLab *)tipeLab{
    if (!_tipeLab) {
        _tipeLab = [BaseLab initCustomLab:@"普通用户" color:[UIColor whiteColor] font:[UIFont fontWithName:@"PingFang-SC-Regular" size:12]];
        if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]) {
            _tipeLab.backgroundColor = Main_Token_Model_MANAGER.backgroundColor ;
        }
        
        _tipeLab.textAlignment = NSTextAlignmentCenter;
        [_tipeLab setCornerRadiusWithRadius:4 isMasks:YES];
    }
    return _tipeLab;
}


//-(MeVCTbvTypeCellView *)shadowView{
//    if (!_shadowView) {
//        _shadowView = [[MeVCTbvTypeCellView alloc]initWithFrame:CGRectMake(0, 0, 0, 0 )];
//        _shadowView.btnPackUp.hidden = YES ;
//    }
//    return _shadowView;
//}


-(BaseLab *)VipLab{
    if (!_VipLab) {
        _VipLab = [BaseLab initCustomLab:@"活跃 Lv0" color:[UIColor whiteColor] font:[UIFont fontWithName:@"PingFang-SC-Regular" size:12]];
        _VipLab.backgroundColor = HexColor(0x999999);
        _VipLab.textAlignment = NSTextAlignmentCenter;
        [_VipLab setCornerRadiusWithRadius:4 isMasks:YES];
    }
    return _VipLab;
}
@end
